package com.hitqz.robot.biz.flow.component.agv;

import com.alibaba.fastjson.JSONObject;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.driver.model.IRobotPosDto;
import com.hitqz.robot.driver.model.RobotIOStatusDto;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Optional;

/**
 * @author xupkun
 * @date 2024/12/22
 */
@Slf4j
@LiteflowComponent(value = "WaitBaofeiLife", name = "[轮对/轮轴]等待报废下线")
@RequiredArgsConstructor
public class WaitBaofeiLife extends NodeComponent {

    private final RobotDriverService robotDriverService;

    private final RobotService robotService;

    @Override
    public void process() throws Exception {
        LiteFlowUtil.checkInterruptFlag();
        FlowContextDto dto =  this.getRequestData();
        FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
                .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        //String targetName = ticketsDto.getModalData().get("targetPosName")== null ?
        //        String.valueOf(ticketsDto.getModalData().get("targetPosCode")):
        //        String.valueOf(ticketsDto.getModalData().get("targetPosName"));
        String sourceName = Optional.ofNullable(ticketsDto.getModalData().get("sourcePosName"))
                .orElse(ticketsDto.getModalData().get("sourcePosCode")).toString();
        String targetName = Optional.ofNullable(ticketsDto.getModalData().get("targetPosName"))
                .orElse(ticketsDto.getModalData().get("targetPosCode")).toString();

        if (targetName.equals("911020")) {
            // 等待报废下线
            log.info("等待报废下线");

            while (true) {
                try {
                    IRobotPosDto iRobotPosDto = robotDriverService.getWebPos();
                    if (iRobotPosDto == null) {
                        continue;
                    }
                    List<RobotIOStatusDto> dtos = iRobotPosDto.getDI();
                    Optional<RobotIOStatusDto> d0 = dtos.stream().filter(t-> t.getId() == 0).findFirst();
                    Optional<RobotIOStatusDto> d1 = dtos.stream().filter(t-> t.getId() == 1).findFirst();
                    Optional<RobotIOStatusDto> d2 = dtos.stream().filter(t-> t.getId() == 2).findFirst();
                    Optional<RobotIOStatusDto> d6 = dtos.stream().filter(t-> t.getId() == 6).findFirst();
                    if (d0.isPresent() && d1.isPresent() && d2.isPresent() && d6.isPresent()) {
                        if ((!d0.get().isStatus()) && (!d1.get().isStatus()) && (!d2.get().isStatus())&& (!d6.get().isStatus())) {
                            log.info("全部吊走了");
                            break;
                        }
                        LiteFlowUtil.checkInterruptFlag();
                    }
                } catch (Exception e) {

                }

            }
        }
    }
}
